<template>
  <div>
    <div class="lableStyleClass">
      <span>1. 请点击 &nbsp;“<span style="color : red">模板</span>” &nbsp;按钮，下载模板。</span>
      <AjaxButton size="mini" type="primary" :loading="exportLoading" icon="el-icon-download" plain :click="downloadTemplate">模 板</AjaxButton>
    </div>
    <div class="lableStyleClass">
      <p>2. 点击“<span style="color : red">选择</span>”按钮，选择要导入的xls文件，内容为：仓库代码、库位代码。</p>
      <!-- <p>2. 点击“<span style="color : red">选择</span>”按钮，选择要导入的xls文件，内容为：仓库代码、库位代码、车间发料仓库标志、停用日期。</p> -->
    </div>
    <!-- <el-upload
      ref="upload"
      class="importPrice"
      action="/dmscloud.part/basedata/LibraryDefinition/importLibrary"
      accept=".xlsx,.xls"
      :limit="1"
      :headers="headersObj"
      :on-success="showErrorList"
      :on-exceed="handleExceed"
    >
      <el-button size="small" type="primary" plain>点击上传</el-button>
    </el-upload> -->

    <div class="lableStyleClass">
      <UploadFile
        :initial-value="fileInitalValue"
        isOnlyButton
        :limit="20"
        :disabled="addBtnDisabled"
        type="primary"
        icon="el-icon-download"
        :actionUrl="`${server.DMSCLOUD_PART}/basedata/LibraryDefinition/importLibrary`"
        @change="handleUploadChange"
        >点击上传</UploadFile
      >
    </div>

    <!-- <UploadFile
    action-url="/api/dmscloud.sysbasedata/vehicleProduct/car/importCarPrice"
    :initial-value="form.fileList"

    @change="successHandler"
    >点击上传
    </UploadFile> -->
  </div>
</template>
<script>
// export default {
//   name: 'aaa',
//   data() {
//     return {
//       form: {
//         fileList: [],
//       }
//     };
//   },
//   methods: {
//     successHandler(val) {
//       this.form.fileList = val;
//     }
//   }
// };
import { getToken, getAppId, getUserId } from '@/assets/js/auth';
import { modelDownload } from '@/api/part/partInfo';
import { exportExcelFile } from '@/utils';
import SERVER from '@/api/server';
export default {
  data() {
    return {
      headersObj: {
        jwt: getToken(),
        appId: getAppId(),
        userId: getUserId(),
        userAgent: 'pc'
      },
      errorList: [],
      fileInitalValue: [], // 上传文件
      addBtnDisabled: false,
      server: SERVER,
      exportLoading: false
    };
  },
  methods: {
    // 导入成功事件
    handleUploadChange(e) {
      if (e != 1) {
        console.log('errorList', e);
        let errorList = _.cloneDeep(e[e.length - 1].url.dataList);
        let updateList = _.cloneDeep(e[e.length - 1].url.updateList);
        let msg = '';
        let msg1 = '';
        let msg2 = '';
        let index = 0;
        let index1 = 0;
        let index2 = 0;
        errorList.forEach(element => {
          if (element['errorMsg']) {
            msg = msg + element['storagePositionCode'] + ',';
            index1++;
          } else {
            msg1 = msg1 + element['storagePositionCode'] + ',';
            index++;
          }
        });
        updateList.forEach(x=>{
          msg2 = msg2 + x['STORAGE_POSITION_CODE'] + ',';
          index2++;
        });
        msg2 = msg2.substring(0, msg2.lastIndexOf(','));
        if (msg1.length > 10) {
          msg1 = msg1.substring(0, 10);
          msg1 = msg1 + '...';
        }
        if (msg.length > 10) {
          msg = msg.substring(0, 10);
          msg = msg + '...';
        }
        if(msg.length==0){
          msg = ',';
        }
        this.$notify.success({ title: '提示信息', message: '成功导入' + index + '条,库位为' + msg1 + '失败' + index1 + '条,库位为' + msg+'更新'+ index2 + '条,库位为'+msg2});
        this.$emit('close', false, 'refresh');
      }

      // this.fileInitalValue = e;
      // let excelData = e[0].url.result;
      // console.log(excelData, 'excelData');
      // console.log(this.fileInitalValue, 'excelDatssssa');
      // if (excelData.length) {
      //   // this.formatPartsData(excelData);
      //   this.handleDetailSelectConfirm(excelData, e[0].url.errorList);
      // }
    },
    downloadTemplate() {
      this.$confirm('此操作将下载excel模板, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.exportLoading = true;
        modelDownload().then(res => {
          // console.log(res);
          exportExcelFile(res.data, '库位定义模板');
          this.exportLoading = false;
        });
      });
    },
    showErrorList(response, file, fileList) {
      this.errorList = response.data;
      if (this.errorList == [] || this.errorList == null) {
        this.$message('上传成功! 0条处理成功');
      } else {
        this.$message('上传成功!  ' + this.errorList.length + '条处理成功');
      }
      this.$emit('close', false, 'refresh');
    },
    handleExceed(files, fileList) {
      this.$message(`只能选择一个文件`);
    }
  }
};
</script>
<style lang="less" scoped>
.lableStyleClass {
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  padding: 10px 20px;
  text-align: left;
}</style
>>
